
from lxml import etree

from curl_cffi import requests
import requests
import time
import requests
import re
#
# cookies = {
#     'Hm_lvt_194df3105ad7148dcf2b98a91b5e727a': '1761966347',
#     'HMACCOUNT': 'D4B399F60924DE51',
#     'sid': 'market_aqc',
#     '__g': 'market_aqc',
#     'lastCity': '101180100',
#     '__zp_seo_uuid__': 'fbe82a9d-81ca-4bb7-a62d-959e89d416aa',
#     'ab_guid': '812522f1-d9ef-4e6f-9360-2f1a920dd58c',
#     'wt2': 'DIy8uu-cmCt1UYZLiDN2Fh8KfNnr4z2nOkX4EGKkKxSIsM4J932-7rgseraZiw9cM7IVd-yUNDXxJKWLhrpMFDA~~',
#     'wbg': '0',
#     'zp_at': 'pm97vtb4C48wC8qtklmlGy3bbzy8qFJalhQfbBi-LGY~',
#     'Hm_lpvt_194df3105ad7148dcf2b98a91b5e727a': '1761978558',
#     'bst': 'V2R94vEeP101liVtRuyR8eKyO07DrSxy0~|R94vEeP101liVtRuyR8eKyO07DrTwS4~',
#     '__c': '1761966349',
#     '__l': 'r=https%3A%2F%2Fwww.google.com%2F&l=%2Fwww.zhipin.com%2Fweb%2Fgeek%2Fjobs%3Fquery%3D%26city%3D101180100%26position%3D100514&s=3&friend_source=0&s=3&friend_source=0',
#     '__a': '69639392.1761966349..1761966349.28.1.28.28',
#     '__zp_stoken__': 'f140fQzzDq8OEwpLDgksyCRANDxc5KT48L8KKQUMwR0VFQzw9S0tDPEUlOTPDvMK9w7nCqEdkw4wrQCw8PTxAQ0o9RUccPEHFh8K5QD84wqLDgMSHwqRBXcOJEi7CvWvDkcK%2BCcKyw4QVQMK%2FEBvDgCkuCcOFOT5FPMOIw4LCnsOIcMOLwpnDh8KIw4N8w4Q%2BPTxDN0JrXVldQj1JV2IMU2VUaGZXDk9WUTM8OUNDwobCrTNCEA8SGhMODQwUCRMMDQ4XDQ4TGxIODQwUCTs8wqfDhMSGw5XErhXDt8SjwpnDiMKqVcKuwrzClHXCpsKxwpfCosKiYsOIw4LCsWbDgMOAa0zDg8OKwrtkwr%2FDiMKqT8OIYcK4VXZawq94d25gY18SFxIXE1w9FsSIZcOJ',
# }
#
# headers = {
#     'accept': 'application/json, text/plain, */*',
#     'accept-language': 'zh-CN,zh;q=0.9',
#     'content-type': 'application/x-www-form-urlencoded',
#     'origin': 'https://www.zhipin.com',
#     'priority': 'u=1, i',
#     'referer': 'https://www.zhipin.com/web/geek/jobs?query=&city=101180100&position=100514',
#     'sec-ch-ua': '"Google Chrome";v="141", "Not?A_Brand";v="8", "Chromium";v="141"',
#     'sec-ch-ua-mobile': '?0',
#     'sec-ch-ua-platform': '"Windows"',
#     'sec-fetch-dest': 'empty',
#     'sec-fetch-mode': 'cors',
#     'sec-fetch-site': 'same-origin',
#     'token': 'ShWd1lktThVQFnBs',
#     'traceid': 'F-1f4a06K9267qOJso',
#     'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36',
#     'x-requested-with': 'XMLHttpRequest',
#     'zp_token': 'V2R94vEeP101liVtRuyR8eKyO07DrSxy0~|R94vEeP101liVtRuyR8eKyO07DrTwS4~',
#     # 'cookie': 'Hm_lvt_194df3105ad7148dcf2b98a91b5e727a=1761966347; HMACCOUNT=D4B399F60924DE51; sid=market_aqc; __g=market_aqc; lastCity=101180100; __zp_seo_uuid__=fbe82a9d-81ca-4bb7-a62d-959e89d416aa; ab_guid=812522f1-d9ef-4e6f-9360-2f1a920dd58c; wt2=DIy8uu-cmCt1UYZLiDN2Fh8KfNnr4z2nOkX4EGKkKxSIsM4J932-7rgseraZiw9cM7IVd-yUNDXxJKWLhrpMFDA~~; wbg=0; zp_at=pm97vtb4C48wC8qtklmlGy3bbzy8qFJalhQfbBi-LGY~; Hm_lpvt_194df3105ad7148dcf2b98a91b5e727a=1761978558; bst=V2R94vEeP101liVtRuyR8eKyO07DrSxy0~|R94vEeP101liVtRuyR8eKyO07DrTwS4~; __c=1761966349; __l=r=https%3A%2F%2Fwww.google.com%2F&l=%2Fwww.zhipin.com%2Fweb%2Fgeek%2Fjobs%3Fquery%3D%26city%3D101180100%26position%3D100514&s=3&friend_source=0&s=3&friend_source=0; __a=69639392.1761966349..1761966349.28.1.28.28; __zp_stoken__=f140fQzzDq8OEwpLDgksyCRANDxc5KT48L8KKQUMwR0VFQzw9S0tDPEUlOTPDvMK9w7nCqEdkw4wrQCw8PTxAQ0o9RUccPEHFh8K5QD84wqLDgMSHwqRBXcOJEi7CvWvDkcK%2BCcKyw4QVQMK%2FEBvDgCkuCcOFOT5FPMOIw4LCnsOIcMOLwpnDh8KIw4N8w4Q%2BPTxDN0JrXVldQj1JV2IMU2VUaGZXDk9WUTM8OUNDwobCrTNCEA8SGhMODQwUCRMMDQ4XDQ4TGxIODQwUCTs8wqfDhMSGw5XErhXDt8SjwpnDiMKqVcKuwrzClHXCpsKxwpfCosKiYsOIw4LCsWbDgMOAa0zDg8OKwrtkwr%2FDiMKqT8OIYcK4VXZawq94d25gY18SFxIXE1w9FsSIZcOJ',
# }
#
# params = {
#     '_': '1761978829318',
# }
#
# data = {
#     'page': '2',
#     'pageSize': '15',
#     'city': '101180100',
#     'position': '100514',
#     'query': '',
#     'expectInfo': '',
#     'multiSubway': '',
#     'multiBusinessDistrict': '',
#     'jobType': '',
#     'salary': '',
#     'experience': '',
#     'degree': '',
#     'industry': '',
#     'scale': '',
#     'stage': '',
#     'scene': '1',
# }
#
# response = requests.post(
#     'https://www.zhipin.com/wapi/zpgeek/search/joblist.json',
#     params=params,
#     cookies=cookies,
#     headers=headers,
#     data=data,
# )
# print(response.text)
#
#

URL_HTML = 'https://www.zhipin.com/web/geek/jobs'
URL_JSON = 'https://www.zhipin.com/wapi/zpgeek/search/joblist.json'

cookies = {  'Hm_lvt_194df3105ad7148dcf2b98a91b5e727a': '1761966347',
    'HMACCOUNT': 'D4B399F60924DE51',
    'sid': 'market_aqc',
    '__g': 'market_aqc',
    'lastCity': '101180100',
    '__zp_seo_uuid__': 'fbe82a9d-81ca-4bb7-a62d-959e89d416aa',
    'ab_guid': '812522f1-d9ef-4e6f-9360-2f1a920dd58c',
    'wt2': 'DIy8uu-cmCt1UYZLiDN2Fh8KfNnr4z2nOkX4EGKkKxSIsM4J932-7rgseraZiw9cM7IVd-yUNDXxJKWLhrpMFDA~~',
    'wbg': '0',
    'zp_at': 'pm97vtb4C48wC8qtklmlGy3bbzy8qFJalhQfbBi-LGY~',
    'Hm_lpvt_194df3105ad7148dcf2b98a91b5e727a': '1761978558',
    'bst': 'V2R94vEeP101liVtRuyR8eKyO07DrSxy0~|R94vEeP101liVtRuyR8eKyO07DrTwS4~',
    '__c': '1761966349',
    '__l': 'r=https%3A%2F%2Fwww.google.com%2F&l=%2Fwww.zhipin.com%2Fweb%2Fgeek%2Fjobs%3Fquery%3D%26city%3D101180100%26position%3D100514&s=3&friend_source=0&s=3&friend_source=0',
    '__a': '69639392.1761966349..1761966349.28.1.28.28',
    '__zp_stoken__': 'f140fQzzDq8OEwpLDgksyCRANDxc5KT48L8KKQUMwR0VFQzw9S0tDPEUlOTPDvMK9w7nCqEdkw4wrQCw8PTxAQ0o9RUccPEHFh8K5QD84wqLDgMSHwqRBXcOJEi7CvWvDkcK%2BCcKyw4QVQMK%2FEBvDgCkuCcOFOT5FPMOIw4LCnsOIcMOLwpnDh8KIw4N8w4Q%2BPTxDN0JrXVldQj1JV2IMU2VUaGZXDk9WUTM8OUNDwobCrTNCEA8SGhMODQwUCRMMDQ4XDQ4TGxIODQwUCTs8wqfDhMSGw5XErhXDt8SjwpnDiMKqVcKuwrzClHXCpsKxwpfCosKiYsOIw4LCsWbDgMOAa0zDg8OKwrtkwr%2FDiMKqT8OIYcK4VXZawq94d25gY18SFxIXE1w9FsSIZcOJ',
}          # 你前面贴的那组长期值
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
    'referer': URL_HTML,
}
s = requests.Session()
s.cookies.update(cookies)
s.headers.update(headers)

# 2. 先拿 HTML 里的 stoken / zp_token
html = s.get(URL_HTML, params={'city': '101180100', 'position': '100514'}).text
stoken = re.search(r'"stoken":"(.*?)"', html).group(1)
zptoken = re.search(r'"zpToken":"(.*?)"', html).group(1)

# 3. 发一次“空” JSON 请求，只为摘 traceid
dummy_params = {'_': str(int(time.time()*1000))}
dummy_data = {'page': '1', 'pageSize': '15', 'city': '101180100', 'position': '100514', 'scene': '1'}

# 第一次 POST 一定 401/空数据，无所谓，我们只拿响应头
r = s.post(URL_JSON, params=dummy_params, data=dummy_data)
traceid = r.headers['traceid']          # ← 关键
print('got traceid:', traceid)

# 4. 把 traceid 写进 headers，再发正式请求
s.headers.update({'traceid': traceid, 'zp_token': zptoken})
s.cookies.update({'__zp_stoken__': stoken, 'bst': zptoken})

real_params = {'_': str(int(time.time()*1000))}
real_data = {'page': '2', 'pageSize': '15', 'city': '101180100', 'position': '100514', 'scene': '1'}

resp = s.post(URL_JSON, params=real_params, data=real_data)
print(resp.json())